package net.fonline.apps.pathfinder;

/**
 * ArrayList-Implementation for JavaME
 * @author Michael
 */
public class ArrayList {

    private ArrayListNode root = null;

    public void add(Node node) {
        if (root == null) {
            root = new ArrayListNode();
            root.node = node;
            root.next = null;
        } else {
            ArrayListNode tmp = root;
            while (tmp.next != null) {
                tmp = tmp.next;
            }
            tmp.next = new ArrayListNode();
            tmp = tmp.next;
            tmp.node = node;
            tmp.next = null;
        }
    }

    public int size() {
        if (root == null) {
            return 0;
        } else {
            ArrayListNode tmp = root;
            int i = 1;
            while (tmp.next != null) {
                tmp = tmp.next;
                i++;
            }
            return i;
        }
    }

    public void remove(Node lowest) {
        ArrayListNode tmp = root;
        if (tmp == null) {
            return;
        }
        if (tmp.node == lowest) {
            root = tmp.next;
            return;
        } else if (tmp.next == null) {
            return;
        }

        while (tmp.next != null && tmp.next.node != lowest) {
            tmp = tmp.next;
        }
        if (tmp.next != null && tmp.next.node == lowest) {
            tmp.next = tmp.next.next;
        }
    }

    public Node get(int i) {
        ArrayListNode node = root;
        for (int it = 0; it < i; it++) {
            if (node == null) {
                return null;
            }
            node = node.next;
        }
        if (node == null) {
            return null;
        }
        return node.node;
    }

    public ArrayList pointer() {
        return this;
    }
}
